using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace MaterialUI
{
	[ExecuteInEditMode]
	[AddComponentMenu("MaterialUI/Screen View", 100)]
	public class ScreenView : UIBehaviour
	{
		public enum SlideDirection
		{
			Left,
			Right,
			Up,
			Down
		}

		public enum RippleType
		{
			MousePosition,
			Manual,
			Center
		}

		public enum Type
		{
			In,
			Out,
			InOut
		}

		[Serializable]
		public class OnScreenTransitionUnityEvent : UnityEvent<int>
		{
		}

		[SerializeField]
		private MaterialScreen[] m_MaterialScreens = new MaterialScreen[0];

		[SerializeField]
		private int m_CurrentScreen;

		private int m_LastScreen;

		[SerializeField]
		private bool m_FadeIn = true;

		[SerializeField]
		private Tween.TweenType m_FadeInTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private float m_FadeInAlpha;

		[SerializeField]
		private AnimationCurve m_FadeInCustomCurve;

		[SerializeField]
		private bool m_ScaleIn;

		[SerializeField]
		private Tween.TweenType m_ScaleInTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private float m_ScaleInScale;

		[SerializeField]
		private AnimationCurve m_ScaleInCustomCurve;

		[SerializeField]
		private bool m_SlideIn;

		[SerializeField]
		private Tween.TweenType m_SlideInTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private SlideDirection m_SlideInDirection = SlideDirection.Right;

		[SerializeField]
		private bool m_AutoSlideInAmount = true;

		[SerializeField]
		private float m_SlideInAmount;

		[SerializeField]
		private float m_SlideInPercent = 100f;

		[SerializeField]
		private AnimationCurve m_SlideInCustomCurve;

		[SerializeField]
		private bool m_RippleIn;

		[SerializeField]
		private Tween.TweenType m_RippleInTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private RippleType m_RippleInType;

		[SerializeField]
		private Vector2 m_RippleInPosition;

		[SerializeField]
		private AnimationCurve m_RippleInCustomCurve;

		[SerializeField]
		private bool m_FadeOut;

		[SerializeField]
		private Tween.TweenType m_FadeOutTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private float m_FadeOutAlpha;

		[SerializeField]
		private AnimationCurve m_FadeOutCustomCurve;

		[SerializeField]
		private bool m_ScaleOut;

		[SerializeField]
		private Tween.TweenType m_ScaleOutTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private float m_ScaleOutScale;

		[SerializeField]
		private AnimationCurve m_ScaleOutCustomCurve;

		[SerializeField]
		private bool m_SlideOut;

		[SerializeField]
		private Tween.TweenType m_SlideOutTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private SlideDirection m_SlideOutDirection;

		[SerializeField]
		private bool m_AutoSlideOutAmount = true;

		[SerializeField]
		private float m_SlideOutAmount;

		[SerializeField]
		private float m_SlideOutPercent = 100f;

		[SerializeField]
		private AnimationCurve m_SlideOutCustomCurve;

		[SerializeField]
		private bool m_RippleOut;

		[SerializeField]
		private Tween.TweenType m_RippleOutTweenType = Tween.TweenType.EaseOutQuint;

		[SerializeField]
		private RippleType m_RippleOutType;

		[SerializeField]
		private Vector2 m_RippleOutPosition;

		[SerializeField]
		private AnimationCurve m_RippleOutCustomCurve;

		[SerializeField]
		private float m_TransitionDuration = 0.5f;

		[SerializeField]
		private Type m_TransitionType;

		private Canvas m_Canvas;

		private GraphicRaycaster m_GraphicRaycaster;

		[SerializeField]
		private OnScreenTransitionUnityEvent m_OnScreenEndTransition;

		[SerializeField]
		private OnScreenTransitionUnityEvent m_OnScreenBeginTransition;

		private int m_ScreensTransitioning;

		public MaterialScreen[] materialScreen
		{
			get
			{
				return m_MaterialScreens;
			}
			set
			{
				m_MaterialScreens = value;
			}
		}

		public int currentScreen
		{
			get
			{
				return m_CurrentScreen;
			}
			set
			{
				m_CurrentScreen = value;
			}
		}

		public int lastScreen => m_LastScreen;

		public bool fadeIn
		{
			get
			{
				return m_FadeIn;
			}
			set
			{
				m_FadeIn = value;
			}
		}

		public Tween.TweenType fadeInTweenType
		{
			get
			{
				return m_FadeInTweenType;
			}
			set
			{
				m_FadeInTweenType = value;
			}
		}

		public float fadeInAlpha
		{
			get
			{
				return m_FadeInAlpha;
			}
			set
			{
				m_FadeInAlpha = value;
			}
		}

		public AnimationCurve fadeInCustomCurve
		{
			get
			{
				return m_FadeInCustomCurve;
			}
			set
			{
				m_FadeInCustomCurve = value;
			}
		}

		public bool scaleIn
		{
			get
			{
				return m_ScaleIn;
			}
			set
			{
				m_ScaleIn = value;
			}
		}

		public Tween.TweenType scaleInTweenType
		{
			get
			{
				return m_ScaleInTweenType;
			}
			set
			{
				m_ScaleInTweenType = value;
			}
		}

		public float scaleInScale
		{
			get
			{
				return m_ScaleInScale;
			}
			set
			{
				m_ScaleInScale = value;
			}
		}

		public AnimationCurve scaleInCustomCurve
		{
			get
			{
				return m_ScaleInCustomCurve;
			}
			set
			{
				m_ScaleInCustomCurve = value;
			}
		}

		public bool slideIn
		{
			get
			{
				return m_SlideIn;
			}
			set
			{
				m_SlideIn = value;
			}
		}

		public Tween.TweenType slideInTweenType
		{
			get
			{
				return m_SlideInTweenType;
			}
			set
			{
				m_SlideInTweenType = value;
			}
		}

		public SlideDirection slideInDirection
		{
			get
			{
				return m_SlideInDirection;
			}
			set
			{
				m_SlideInDirection = value;
			}
		}

		public bool autoSlideInAmount
		{
			get
			{
				return m_AutoSlideInAmount;
			}
			set
			{
				m_AutoSlideInAmount = value;
			}
		}

		public float slideInAmount
		{
			get
			{
				return m_SlideInAmount;
			}
			set
			{
				m_SlideInAmount = value;
			}
		}

		public float slideInPercent
		{
			get
			{
				return m_SlideInPercent;
			}
			set
			{
				m_SlideInPercent = value;
			}
		}

		public AnimationCurve slideInCustomCurve
		{
			get
			{
				return m_SlideInCustomCurve;
			}
			set
			{
				m_SlideInCustomCurve = value;
			}
		}

		public bool rippleIn
		{
			get
			{
				return m_RippleIn;
			}
			set
			{
				m_RippleIn = value;
			}
		}

		public Tween.TweenType rippleInTweenType
		{
			get
			{
				return m_RippleInTweenType;
			}
			set
			{
				m_RippleInTweenType = value;
			}
		}

		public RippleType rippleInType
		{
			get
			{
				return m_RippleInType;
			}
			set
			{
				m_RippleInType = value;
			}
		}

		public Vector2 rippleInPosition
		{
			get
			{
				return m_RippleInPosition;
			}
			set
			{
				m_RippleInPosition = value;
			}
		}

		public AnimationCurve rippleInCustomCurve
		{
			get
			{
				return m_RippleInCustomCurve;
			}
			set
			{
				m_RippleInCustomCurve = value;
			}
		}

		public bool fadeOut
		{
			get
			{
				return m_FadeOut;
			}
			set
			{
				m_FadeOut = value;
			}
		}

		public Tween.TweenType fadeOutTweenType
		{
			get
			{
				return m_FadeOutTweenType;
			}
			set
			{
				m_FadeOutTweenType = value;
			}
		}

		public float fadeOutAlpha
		{
			get
			{
				return m_FadeOutAlpha;
			}
			set
			{
				m_FadeOutAlpha = value;
			}
		}

		public AnimationCurve fadeOutCustomCurve
		{
			get
			{
				return m_FadeOutCustomCurve;
			}
			set
			{
				m_FadeOutCustomCurve = value;
			}
		}

		public bool scaleOut
		{
			get
			{
				return m_ScaleOut;
			}
			set
			{
				m_ScaleOut = value;
			}
		}

		public Tween.TweenType scaleOutTweenType
		{
			get
			{
				return m_ScaleOutTweenType;
			}
			set
			{
				m_ScaleOutTweenType = value;
			}
		}

		public float scaleOutScale
		{
			get
			{
				return m_ScaleOutScale;
			}
			set
			{
				m_ScaleOutScale = value;
			}
		}

		public AnimationCurve scaleOutCustomCurve
		{
			get
			{
				return m_ScaleOutCustomCurve;
			}
			set
			{
				m_ScaleOutCustomCurve = value;
			}
		}

		public bool slideOut
		{
			get
			{
				return m_SlideOut;
			}
			set
			{
				m_SlideOut = value;
			}
		}

		public Tween.TweenType slideOutTweenType
		{
			get
			{
				return m_SlideOutTweenType;
			}
			set
			{
				m_SlideOutTweenType = value;
			}
		}

		public SlideDirection slideOutDirection
		{
			get
			{
				return m_SlideOutDirection;
			}
			set
			{
				m_SlideOutDirection = value;
			}
		}

		public bool autoSlideOutAmount
		{
			get
			{
				return m_AutoSlideOutAmount;
			}
			set
			{
				m_AutoSlideOutAmount = value;
			}
		}

		public float slideOutAmount
		{
			get
			{
				return m_SlideOutAmount;
			}
			set
			{
				m_SlideOutAmount = value;
			}
		}

		public float slideOutPercent
		{
			get
			{
				return m_SlideOutPercent;
			}
			set
			{
				m_SlideOutPercent = value;
			}
		}

		public AnimationCurve slideOutCustomCurve
		{
			get
			{
				return m_SlideOutCustomCurve;
			}
			set
			{
				m_SlideOutCustomCurve = value;
			}
		}

		public bool rippleOut
		{
			get
			{
				return m_RippleOut;
			}
			set
			{
				m_RippleOut = value;
			}
		}

		public Tween.TweenType rippleOutTweenType
		{
			get
			{
				return m_RippleOutTweenType;
			}
			set
			{
				m_RippleOutTweenType = value;
			}
		}

		public RippleType rippleOutType
		{
			get
			{
				return m_RippleOutType;
			}
			set
			{
				m_RippleOutType = value;
			}
		}

		public Vector2 rippleOutPosition
		{
			get
			{
				return m_RippleOutPosition;
			}
			set
			{
				m_RippleOutPosition = value;
			}
		}

		public AnimationCurve rippleOutCustomCurve
		{
			get
			{
				return m_RippleOutCustomCurve;
			}
			set
			{
				m_RippleOutCustomCurve = value;
			}
		}

		public float transitionDuration
		{
			get
			{
				return m_TransitionDuration;
			}
			set
			{
				m_TransitionDuration = value;
			}
		}

		public Type transitionType
		{
			get
			{
				return m_TransitionType;
			}
			set
			{
				m_TransitionType = value;
			}
		}

		public Canvas canvas
		{
			get
			{
				return m_Canvas;
			}
			set
			{
				m_Canvas = value;
			}
		}

		public GraphicRaycaster graphicRaycaster
		{
			get
			{
				return m_GraphicRaycaster;
			}
			set
			{
				m_GraphicRaycaster = value;
			}
		}

		public OnScreenTransitionUnityEvent onScreenEndTransition
		{
			get
			{
				return m_OnScreenEndTransition;
			}
			set
			{
				m_OnScreenEndTransition = value;
			}
		}

		public OnScreenTransitionUnityEvent onScreenBeginTransition
		{
			get
			{
				return m_OnScreenBeginTransition;
			}
			set
			{
				m_OnScreenBeginTransition = value;
			}
		}

		protected override void Start()
		{
			if (!Application.isPlaying || m_MaterialScreens.Length <= 0)
			{
				return;
			}
			for (int i = 0; i < materialScreen.Length; i++)
			{
				if (i != m_CurrentScreen)
				{
					materialScreen[i].gameObject.SetActive(!materialScreen[i].disableWhenNotVisible);
				}
				m_MaterialScreens[i].screenIndex = i;
			}
			m_MaterialScreens[m_CurrentScreen].gameObject.SetActive(value: true);
			m_MaterialScreens[m_CurrentScreen].rectTransform.SetAsLastSibling();
		}

		public void Back()
		{
			Transition(m_LastScreen);
		}

		public void Back(Type transitionType)
		{
			Transition(m_LastScreen, transitionType);
		}

		public void Transition(int screenIndex)
		{
			Transition(screenIndex, transitionType);
		}

		public void Transition(int screenIndex, Type transitionType)
		{
			if (0 <= screenIndex && screenIndex < materialScreen.Length && screenIndex != currentScreen)
			{
				m_LastScreen = m_CurrentScreen;
				m_CurrentScreen = screenIndex;
				m_MaterialScreens[m_LastScreen].Interrupt();
				m_MaterialScreens[m_CurrentScreen].Interrupt();
				switch (transitionType)
				{
				case Type.In:
					m_MaterialScreens[m_CurrentScreen].rectTransform.SetAsLastSibling();
					m_MaterialScreens[m_CurrentScreen].TransitionIn();
					m_MaterialScreens[m_LastScreen].TransitionOutWithoutTransition();
					break;
				case Type.Out:
					m_MaterialScreens[m_CurrentScreen].rectTransform.SetAsLastSibling();
					m_MaterialScreens[m_LastScreen].rectTransform.SetAsLastSibling();
					m_MaterialScreens[m_CurrentScreen].gameObject.SetActive(value: true);
					m_MaterialScreens[m_LastScreen].TransitionOut();
					break;
				case Type.InOut:
					m_MaterialScreens[m_CurrentScreen].rectTransform.SetAsLastSibling();
					m_MaterialScreens[m_CurrentScreen].TransitionIn();
					m_MaterialScreens[m_LastScreen].TransitionOut();
					break;
				}
				m_ScreensTransitioning += 2;
				m_OnScreenBeginTransition.InvokeIfNotNull(screenIndex);
			}
		}

		public void NextScreen(bool wrap = true)
		{
			if (currentScreen != materialScreen.Length - 1)
			{
				Transition(currentScreen + 1);
			}
			else if (wrap)
			{
				Transition(0);
			}
		}

		public void PreviousScreen(bool wrap = true)
		{
			if (currentScreen >= 1)
			{
				Transition(currentScreen - 1);
			}
			else if (wrap)
			{
				Transition(materialScreen.Length - 1);
			}
		}

		public void Transition(string screenName)
		{
			Transition(screenName, transitionType);
		}

		public void Transition(string screenName, Type transitionType)
		{
			for (int i = 0; i < materialScreen.Length; i++)
			{
				if (materialScreen[i].name == screenName)
				{
					Transition(i, transitionType);
				}
			}
		}

		public void OnScreenEndTransition(int screenIndex)
		{
			m_ScreensTransitioning--;
			if (m_ScreensTransitioning <= 0)
			{
				m_ScreensTransitioning = 0;
				m_OnScreenEndTransition.InvokeIfNotNull(screenIndex);
				if (m_GraphicRaycaster != null)
				{
					UnityEngine.Object.Destroy(m_GraphicRaycaster);
				}
				if (m_Canvas != null)
				{
					UnityEngine.Object.Destroy(m_Canvas);
				}
			}
		}
	}
}
